﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Homebrew_SD_Manager
{
    public partial class EditorMeta : Form
    {
        public EditorMeta()
        {
            InitializeComponent();
        }

        private void EditorMeta_Load(object sender, EventArgs e)
        {
            initHomebrew();
            init_back(Homebrew_SD_Manager.Properties.Settings.Default.SettingBackColor);
            init_fore(Homebrew_SD_Manager.Properties.Settings.Default.SettingForeColor);
        }

        private void init_back(int color)
        {
            if (color != 0)
            {
                BackColor = Color.FromArgb(color);
                tbCoder.BackColor = Color.FromArgb(color);
                tbLong.BackColor = Color.FromArgb(color);
                tbName.BackColor = Color.FromArgb(color);
                tbRelease.BackColor = Color.FromArgb(color);
                tbShort.BackColor = Color.FromArgb(color);
                tbVersion.BackColor = Color.FromArgb(color);
                lbHomebrew.BackColor = Color.FromArgb(color);
                btSave.BackColor = Color.FromArgb(color);
            }
        }

        private void init_fore(int color)
        {
            if (color != 0)
            {
                ForeColor = Color.FromArgb(color);
                tbCoder.ForeColor = Color.FromArgb(color);
                tbLong.ForeColor = Color.FromArgb(color);
                tbName.ForeColor = Color.FromArgb(color);
                tbRelease.ForeColor = Color.FromArgb(color);
                tbShort.ForeColor = Color.FromArgb(color);
                tbVersion.ForeColor = Color.FromArgb(color);
                lbHomebrew.ForeColor = Color.FromArgb(color);
                btSave.ForeColor = Color.FromArgb(color);
            }
        }

        private void initHomebrew()
        {
            DirectoryInfo di = new DirectoryInfo(@"apps");
            foreach (DirectoryInfo dir in di.GetDirectories())
                lbHomebrew.Items.Add(dir.Name);
        }

        private void readXML()
        {
            try
            {
                XmlTextReader xtr = new XmlTextReader(@"apps\" + lbHomebrew.SelectedItems[0] + @"\meta.xml");
                while (xtr.Read())
                {
                    if (xtr.Name == "name")
                        tbName.Text = xtr.ReadString();
                    else if (xtr.Name == "coder")
                        tbCoder.Text = xtr.ReadString();
                    else if (xtr.Name == "version")
                        tbVersion.Text = xtr.ReadString();
                    else if (xtr.Name == "release_date")
                        tbRelease.Text = xtr.ReadString();
                    else if (xtr.Name == "short_description")
                        tbShort.Text = xtr.ReadString();
                    else if (xtr.Name == "long_description")
                        tbLong.Text = xtr.ReadString();
                }
                xtr.Close();
            }
            catch (Exception e)
            {
                tbLong.Text = e.Message;
            }
        }

        private void writeXML()
        {
            XmlTextWriter xtw = new XmlTextWriter(@"apps\" + lbHomebrew.SelectedItems[0] + @"\meta.xml", Encoding.UTF8);
            xtw.Formatting = Formatting.Indented;
            xtw.Indentation = 2;
            xtw.WriteStartDocument(true);
            xtw.WriteStartElement("app");

            xtw.WriteStartAttribute("version");
            xtw.WriteString(tbVersion.Text);
            xtw.WriteEndAttribute();

            xtw.WriteStartElement("name");
            xtw.WriteString(tbName.Text);
            xtw.WriteEndElement();

            xtw.WriteStartElement("coder");
            xtw.WriteString(tbCoder.Text);
            xtw.WriteEndElement();

            xtw.WriteStartElement("version");
            xtw.WriteString(tbVersion.Text);
            xtw.WriteEndElement();

            xtw.WriteStartElement("release_date");
            xtw.WriteString(tbRelease.Text);
            xtw.WriteEndElement();

            xtw.WriteStartElement("short_description");
            xtw.WriteString(tbShort.Text);
            xtw.WriteEndElement();

            xtw.WriteStartElement("long_description");
            xtw.WriteString(tbLong.Text);
            xtw.WriteEndElement();

            xtw.WriteEndElement();
            xtw.WriteEndDocument();

            xtw.Flush();
            xtw.Close();
        }

        private void lbHomebrew_SelectedIndexChanged(object sender, EventArgs e)
        {
            readXML();
            btSave.Enabled = true;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            writeXML();
        }
    }
}
